package com.luo.divview1;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 自定义的View
 * <p>
 * Created by Luo on 2017/4/26 0026.
 */
public class CustomView extends View {
    /*文本*/
    private String mTitleText;
    /*文本颜色*/
    private int mTitleTextColor;
    /*文本大小*/
    private int mTitleTextSize;

    /*绘制文本的时候的范围*/
    private Rect mBound;
    /*画笔*/
    private Paint mPaint;

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 获取我们自定义的属性
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // obtainStyledAttributes: 1：属性值的集合 2：我们要获取的属性的资源ID的一个数组
        // 3：这个是当前Theme中的一个attribute 4：这个也是指向一个Style的资源ID，但是仅在defStyleAttr为0或defStyleAttr不为0但Theme中没有为defStyleAttr属性赋值时起作用
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyleAttr, 0);
        int n = a.getIndexCount();
        // 遍历自定义的属性
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CustomTitleView_titleText:
                    // 这个不能设置默认值
                    mTitleText = a.getString(attr);
                    break;
                case R.styleable.CustomTitleView_titleTextColor:
                    // 默认颜色为黑色
                    mTitleTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomTitleView_titleTextSize:
                    // 默认为16sp,TypeValue把sp转化成px
                    mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
            // 回收该资源，确保之后不会调用该资源再进行回收
            a.recycle();

            /*获取绘制文本的宽高*/
            mPaint = new Paint();
            mPaint.setTextSize(mTitleTextSize);
            mBound = new Rect();
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 获取宽度的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        // 获取宽度尺寸
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        // 获取高度的模式
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        // 获取高度的尺寸
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;
        // 控件的宽度为一个确确的值，那么就直接将值给宽度
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            // 为画笔设置textSize
            mPaint.setTextSize(mTitleTextSize);
            // 将文字的宽高范围设置给 mBound
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            // 拿到文字内容的宽度
            float textWidth = mBound.width();
            // 将Padding的宽高也设置进来，将之给最后的宽度
            width = (int) (getPaddingLeft() + textWidth + getPaddingRight());
        }

        // 控件的高度为一个确确的值，那么就直接将值给高度
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textHeight = mBound.height();
            height = (int) (getPaddingTop() + textHeight + getPaddingBottom());
        }
        // 将控件的宽高设置一下
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 先画个黄色的背景
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        // 画字
        mPaint.setColor(mTitleTextColor);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
    }

}
